package com.yzy.swagger;

import com.yzy.domain.entity.annotation.PassToken;
import com.yzy.properties.GlobalProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class Swagger {

    @Value(value = "${swagger.enable:false}")
    private Boolean isEnableSwagger;

    @Resource
    private GlobalProperties globalProperties;

    @Bean
    @PassToken
    public Docket buildAPIManageWeb() {
        ParameterBuilder authorizationBuilder = new ParameterBuilder();
        authorizationBuilder.name(globalProperties.getTokenHeader()).modelRef(new ModelRef("string")).parameterType("header").description("Bearer Token").required(false).build();

        List<Parameter> parameters = new ArrayList<Parameter>();
        parameters.add(authorizationBuilder.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yzy.controller"))
                .paths(PathSelectors.any())
                .build()
                .pathMapping("")
                .apiInfo(swaggerConfigInfo())
                .globalOperationParameters(parameters)
                .enable(isEnableSwagger);
    }

    private ApiInfo swaggerConfigInfo() {
        return new ApiInfoBuilder().title("XYHJ-VISION RESTFUL API")
                .description("XYHJ-VISION API Testing")
                .termsOfServiceUrl("http://www.yg.com")
                .version("1.0.0")
                .build();
    }
}